package com.cimpm.permission.service.impl;

import com.cimpm.permission.entity.User;
import com.hby.common.callback.AutoProcessControllerPart;
import com.hby.common.entities.ResMsg;
import com.hby.common.entities.aspect.parameter.ParamConverter;
import com.hby.common.entities.aspect.parameter.ServiceInfo;
import com.hby.common.services.Impl.mian.AutoEntityServiceImpl;
import com.hby.common.utils.JwtUtil;
import org.springframework.stereotype.Service;

import javax.security.auth.login.LoginException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl extends AutoEntityServiceImpl<User> {

    public Object login(ParamConverter parameter, Integer userId, String userName) throws Throwable {
        List<User> users = getEntitiesByMap(parameter.getParamMap(), false, false).getPageData();
        if (users.size() == 1) {
            Map<String, String> map = new HashMap<>();
            map.put("userId", users.get(0).getId().toString());
            map.put("userName", users.get(0).getName());
            String token = JwtUtil.getToken(map);
            return ResMsg.successResOnlyToken(token);
        } else {
            throw new LoginException("用户名或密码不正确");
        }
    }


    @Override
    public void customService(AutoProcessControllerPart autoProcessControllerPart, ServiceInfo serviceInfo) throws Throwable {

    }

    @Override
    public void addEntityOtherToRedis(User user) {

    }

    @Override
    public void delEntityOtherFromRedis(User user) {

    }
}
